package com.zd.controller;/**
 * @Description: DeparturelistController
 * @Author:pioneer
 * @Date: 2021-07-07
 * @Version:v1.0
 */

import com.zd.service.lnter.*;
import com.zzg.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: DeparturelistController
 * @Author:pioneer
 * @Date: 2021-07-07
 * @Version:v1.0
 */
@Controller
public class DeparturelistController {
    @Autowired
    private DeparturelistServicelnter departurelistServicelnter;
    @Autowired
    private SubstationServicelnter substationServicelnter;
    @Autowired
    private TransportProtocolServicelnter transportServicelnter;
    @Autowired
    private FreightinformationServicelnter freigServicelnter;
    @Autowired
    private WaybillServicelnter waybilllnter;
    @Autowired
    private WaybillstateServicelnter waybillstateServicelnter;
    @Autowired
    private TailafterServicelnter tailafterServicelnter;
    @Autowired
    private OutsourcingServicelnter outsourcingServicelnter;
    @Autowired
    private LogisticszdServicelnter logisticszdServicelnter;
    @Autowired
    private ConsignerServicelnter consignerServicelnter;
    @Autowired
    private ConsigneeServicelnter consigneeServicelnter;
    @Autowired
    private DriverServicelnter driverServicelnter;

    /**
     * 查询所有发车列表信息
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("queryinformation")
    @ResponseBody
    public Map<String,Object> queryinformation(int page, int limit){
        System.out.println ("queryinformation进：page"+page+"\tlimit"+limit);
        Map<String,Object> map = new HashMap<> ();
        map.put("code","");
        map.put("msg","");
        map.put("count",departurelistServicelnter.selectDeparturelist ().size ());
        map.put("data",departurelistServicelnter.pagingqueryDeparturelist (page,limit));
        System.out.println (map);
        return map;
    }

    /**
     * 查询所有分站信息
     * @return
     */
    @RequestMapping("querysubseries")
    @ResponseBody
    public List<Substation> querysubseries(){
        List<Substation> substationList= substationServicelnter.selectSubstation ();
        System.out.println ("querysubseries:\t"+substationList);
        return substationList;
    };


    /**
     * 通过运单id查询运单所有信息
     * @param checkenumber 运单id
     * @return
     */
    @RequestMapping("getDeparturelistByid")
    @ResponseBody
    public Departurelist getDeparturelistByid(String checkenumber){
        System.out.println ("getDeparturelistByid:进"+checkenumber);
        Departurelist departurelist=departurelistServicelnter.getDeparturelistByid (checkenumber);
        System.out.println (departurelist);
        return departurelist;
    };

    /**
     * 通过外包id查询外包
     * @param outsourcingnumber
     * @return
     */
    @RequestMapping("getOutsourcing")
    public String getOutsourcing(String outsourcingnumber){
        System.out.println ("getOutsourcing:进"+outsourcingnumber);
        List<Outsourcing> outsourcingList=outsourcingServicelnter.getOutsourcingByid (outsourcingnumber);
        if(outsourcingList!=null){
            return "有该外包信息";
        }
        return "没有";
    };



    /**
     * 多条件查询运输列表信息所有信息
     * @return map集合
     */
    @RequestMapping("conditionDeparturelist")
    @ResponseBody
    public Map<String,Object> conditionDeparturelist(Departurelist departurelist){
        int page=1;
        int limit=10;
        Map<String,Object> map = new HashMap<> ();
        map.put("code","");
        map.put("msg","");
        map.put("count",departurelistServicelnter.getDeparturelistcondition (departurelist));
        map.put("data",departurelistServicelnter.conditionqueryDeparturelist (departurelist,page,limit));
        System.out.println (map);
        return map;
    }

    /**
     * 运单作废
     * @param waybillnumber 运单id
     * @param response 返回
     * @throws IOException
     */
    @RequestMapping("delectDeparturelist")
    public void delectDeparturelist(String waybillnumber, HttpServletResponse response) throws IOException {
        System.out.println ("delectDeparturelist ：进"+waybillnumber);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(waybillnumber!=null){
            int ser =waybilllnter.delectWaybillByid (waybillnumber);
            freigServicelnter.deletefreightInformation (waybillnumber);
            transportServicelnter.delectagreeByid (waybillnumber);
            waybillstateServicelnter.delectWaybillstate (waybillnumber);
            System.out.println (ser);
            if(ser>=0){
                out.write("删除成功");
            }else{
                out.write("删除失败");
            }
        }
    }

    /**
     * 运单跟踪
     * @param waybillnumber 运单id
     * @return
     */
    @RequestMapping("getTailafter")
    @ResponseBody
    public Map<String, Tailafter> getTailafter(String waybillnumber){
        Tailafter tailafter=tailafterServicelnter.idquery (waybillnumber);
        Map<String, Tailafter> map=new HashMap<> ();
        if(tailafter==null){
            Tailafter tailafter1=new Tailafter ();
            tailafter1.setWaybillId (waybillnumber);
            map.put ("Tailafter",tailafter1);
        }else {
            map.put ("Tailafter",tailafter);
        }
        System.out.println (map);
        return map;
    }

    /**
     * 新增外包信息
     * @param
     * @throws IOException
     */
    @RequestMapping("addoutsourcing")
    public void increaseoutsourcing(String outsourcingId,String logisticsId,Double outsourcingorder,Double freight,Double outsourcingNowclosed,Double modepayment,Double recycle ,String freightinformation,String subcontractdate,String type, HttpServletResponse response) throws IOException {
        Outsourcing outsourcing=new Outsourcing ();
        outsourcing.setOutsourcingId (outsourcingId);
        outsourcing.setLogisticsId (logisticsId);
        outsourcing.setFreightinformation (freightinformation);
        outsourcing.setOutsourcingorder (outsourcingorder);
        outsourcing.setFreight (freight);
        outsourcing.setOutsourcingNowclosed (outsourcingNowclosed);
        outsourcing.setModepayment (modepayment);
        outsourcing.setRecycle (recycle);
        outsourcing.setSubcontractdate (subcontractdate);
        outsourcing.setType (type);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
            int value=outsourcingServicelnter.insertoutsourcing (outsourcing);
            if(value>=0){
                out.write("新增外包成功");
            }else{
                out.write("新增外包失败");
            }

    }

    /**
     *查询所有物流信息
     * @return 结果集
     */
    @RequestMapping("queryLogistics")
    @ResponseBody
    public List<Logistics> queryLogistics(){
        List<Logistics> logisticsList=logisticszdServicelnter.selectLogistics ();
        System.out.println ("queryLogistics:\t"+logisticsList);
        return logisticsList;
    }

    @RequestMapping("updateDeparturelist")
    public void updateDeparturelist(Departurelist departurelist,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        System.out.println ("updateDeparturelist\t进："+departurelist.getYundanstate ()+"\t运输类型"+departurelist.getTransporttype ()+"\t托运日期"+departurelist.getCheckedate ());
        Waybill waybill=waybilllnter.selectWaybillByid (departurelist.getCheckenumber ());
        Driver driver1=driverServicelnter.getDriverByid (departurelist.getTransportcarnumber ());
        int ser=waybillstateServicelnter.updatestateBy (departurelist.getYundanstate (),departurelist.getCheckenumber ());
        int one=waybilllnter.updateWaybillByid (departurelist.getCheckenumber (),departurelist.getCheckedate (),departurelist.getTransporttype (),departurelist.getDeliverymethod ());
        int ren=freigServicelnter.updateFreightinformation (departurelist.getOrdersnumber (),departurelist.getPromisedate (),departurelist.getGoodsname (),departurelist.getGoodsquantity (),departurelist.getWrapmanner (),departurelist.getGoodsweight(),departurelist.getGoodsvolume (),departurelist.getGoodsunitprice (),departurelist.getBillingmanner (),departurelist.getBasicfreight (),departurelist.getPickupfee (),departurelist.getDeliveryfees (),departurelist.getGoodsvalue (),departurelist.getGoodsinsured (),departurelist.getUnloadingfees (),departurelist.getOtherfees (),departurelist.getCostcombined (),departurelist.getPayasyougo (),departurelist.getPaymentmethod (),departurelist.getPaydelivery (),departurelist.getGoodsremarks (),departurelist.getGoodsnumber ());
        int res=consignerServicelnter.updateConsigner (departurelist.getConsigners(),departurelist.getShippingphone (),departurelist.getShippingunits(),waybill.getConsignerId ());
        int val=consigneeServicelnter.modifyConsignee (departurelist.getConsignees(),departurelist.getReceivingphone (),departurelist.getReceivingunit(),waybill.getConsigneeId ());
        int driver=driverServicelnter.updateDriver (departurelist.getTransportcarnumber (),departurelist.getDrivername(),departurelist.getDriverphone ());
        int substation=substationServicelnter.updateSubstation (departurelist.getStoragesite(),driver1.getSubId ());
        int haha=logisticszdServicelnter.updateLogistics (departurelist.getOutsourcingcompany(),departurelist.getOutsourcephone(),departurelist.getGoodsnumber ());
        if(ser>=0){
            out.write("修改成功");
        }else{
            out.write("修改失败");
        }
    }
}
